/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ke.me.muhia.spring.masomo.provisioning.xmlrpc;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.io.IOUtils;
import org.apache.xmlrpc.XmlRpcException;
import org.slf4j.Logger;

/**
 *
 * @author Kenneth
 */
public class CustomLoggingUtils {

    public static void logRequest(Logger logger, RequestEntity requestEntity) throws XmlRpcException {
        ByteArrayOutputStream baos = null;
        try {

            logger.debug("----Request-----");
            baos = new ByteArrayOutputStream();
            requestEntity.writeRequest(baos);
            logger.debug(toPrettyXml(logger, baos.toString()));
        } catch (IOException ex) {
            throw new XmlRpcException(ex.getMessage(), ex);
        } finally {
            IOUtils.closeQuietly(baos);

        }
    }

    public static void logRequest(Logger logger, String content) {
        logger.debug("---- Request ----");
        logger.debug(toPrettyXml(logger, content));
    }

    public static String logResponse(Logger logger, InputStream istream) throws XmlRpcException {
        BufferedReader reader = null;
        try {

            reader = new BufferedReader(new InputStreamReader(istream));
            String line = "";

            StringBuilder respBuff = new StringBuilder();

            while ((line = reader.readLine()) != null) {
                respBuff.append(line);
            }

            String response = respBuff.toString();
            logger.debug("-----Response-----");
            logger.debug(toPrettyXml(logger, respBuff.toString()));
            return response;

        } catch (IOException ex) {
            throw  new XmlRpcException(ex.getMessage(), ex);

        } finally {
            IOUtils.closeQuietly(reader);
        }

    }
    
    public static void logResponse(Logger logger, String content) {
        logger.debug("-----Response------");
        logger.debug(toPrettyXml(logger, content));
        
    }

    private static String toPrettyXml(Logger logger, String xml) {
        try {
            Transformer transformer
                    = TransformerFactory.newInstance().newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
            StreamResult result = new StreamResult(new StringWriter());
            StreamSource source = new StreamSource(new StringReader(xml));
            transformer.transform(source, result);
            return result.getWriter().toString();
        } catch (IllegalArgumentException | TransformerException e) {
            logger.warn("Can't parse XML");
            return xml;
        }
    }

}
